package com.sum.tea.xinlian

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.contants.AppConfig
import com.sum.tea.databinding.ActivityTab1TablelayoutDebugBinding
import com.sum.tea.xinlian.viewmodel.SubDebugViewModel

class SubDebugActivity : BaseMvvmActivity<ActivityTab1TablelayoutDebugBinding, SubDebugViewModel>() {

    override fun initView(savedInstanceState: Bundle?) {
        // 初始化ViewModel
        mViewModel.initialize(application)

        // 设置Spinner适配器
        val pdaTypes = mViewModel.getPdaTypes()
        val baudRates = mViewModel.getBaudRates()

        val pdaAdapter = ArrayAdapter(this, R.layout.simple_spinner_item, pdaTypes)
        pdaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.spinnerPdatype2.adapter = pdaAdapter

        val baudAdapter = ArrayAdapter(this, R.layout.simple_spinner_item, baudRates)
        baudAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.spinnerBaudrate.adapter = baudAdapter
        mBinding.spinnerBaudrate.setSelection(5)

        // 设置按钮状态
        mBinding.buttonPbaudr.isEnabled = mViewModel.hasInitialized()

        // 观察接收数据
        mViewModel.receivedData.observe(this@SubDebugActivity) { mess ->
            mBinding.editTextRevd.setText(mess)
        }

        // 观察错误信息或其他消息
        mViewModel.toastMessages.observe(this@SubDebugActivity) { message ->
            Toast.makeText(this@SubDebugActivity, message, Toast.LENGTH_SHORT).show()
        }

        // 设置按钮点击事件
        mBinding.buttonOpen.setOnClickListener {
            val addr = mBinding.editTextDaddr.text.toString()
            val baudRate = mBinding.spinnerBaudrate.selectedItem.toString().toInt()
            mViewModel.open(addr, baudRate)
        }

        mBinding.buttonClose.setOnClickListener {
            mViewModel.close()
        }

        mBinding.buttonSend1.setOnClickListener {
            val strVal = mBinding.editTextSend1.text.toString()
            mViewModel.sendData(strVal)
        }

        mBinding.buttonSend2.setOnClickListener {
            val strVal = mBinding.editTextSend2.text.toString()
            mViewModel.sendData(strVal)
        }

        mBinding.buttonSend3.setOnClickListener {
            val strVal = mBinding.editTextSend3.text.toString()
            mViewModel.sendData(strVal)
        }

        mBinding.buttonRevd.setOnClickListener {
            if (mBinding.buttonRevd.text.toString() == AppConfig.Constr_subdbrev) {
                mViewModel.startReceiving()
                mBinding.buttonRevd.text = AppConfig.Constr_stop
            } else {
                mViewModel.stopReceiving()
                mBinding.buttonRevd.text = AppConfig.Constr_subdbrev
            }
        }

        mBinding.buttonUp.setOnClickListener {
            mViewModel.powerUp()
        }

        mBinding.buttonDown.setOnClickListener {
            mViewModel.powerDown()
        }

        mBinding.buttonPbaudr.setOnClickListener {
            mViewModel.setBaudRate(mBinding.spinnerBaudrate.selectedItem.toString().toInt())
        }

        mBinding.buttonDebugclear.setOnClickListener {
            mViewModel.clearReceivedData()
        }

        mBinding.spinnerPdatype2.onItemSelectedListener = object : android.widget.AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: android.widget.AdapterView<*>,
                view: android.view.View?,
                position: Int,
                id: Long
            ) {
                mBinding.editTextDaddr.setText(mViewModel.getDevPath())
            }

            override fun onNothingSelected(parent: android.widget.AdapterView<*>) {
                // Do nothing
            }
        }
    }
}
